/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _DAL_FASTDDS_DBH_WRITER_HPP_
#define _DAL_FASTDDS_DBH_WRITER_HPP_

#include <fastdds/dds/domain/DomainParticipant.hpp>
#include <fastdds/dds/publisher/Publisher.hpp>
#include <fastdds/dds/subscriber/Subscriber.hpp>
#include <fastdds/dds/publisher/DataWriter.hpp>
#include <fastdds/dds/subscriber/DataReader.hpp>

#include "dal_dmabufheap/DmaBufHeapManager.hpp"
#include "dal_dmabufheap/BufferDescriptorRingBuffer.hpp"
#include "dal_dmabufheap/DbhNotifyMsgDef.hpp"
#include "dal_dmabufheap/FdStrConverter.hpp"
#include "dal_fastdds/listener_impl/DataWriterListenerImpl.hpp"
#include "dal_builtin/msg/dal_dma_buf_heap_send_notify_msg.hpp"
#include "dal_builtin/msg/dal_dma_buf_heap_info.hpp"
#include "dalidl_typesupport_fastrtps_cpp/message_type_support.h"
#include "dal_fastrtps_cpp/type_support_common.hpp"

using namespace eprosima::fastdds::dds;
using namespace dal_dmabufheap;


class DalDbhWriter
{
public:
    DalDbhWriter();
    virtual ~DalDbhWriter();

    void SetFastddsPublisher(Publisher*& publisher);
    void SetFastddsDomainParticipant(DomainParticipant*& participant);

    void SetDbhSendNotifyWriterListener(DataWriterListenerImpl* listener);
    void SetDbhSendNotifyWriterQos(const DataWriterQos& qos);
    void SetDbhSendNotifyWriterStatusMask(const StatusMask& mask);
    void SetTypeSupport(const void* ts);
    void SetOriginTopicName(const std::string& origin_topic_name);

    bool InitRingBuffer(void* msg);
    bool CheckResize(void* msg);
    DataWriter* GetDbhSendNotifyWriter();
    bool CreateDbhBuiltinEntities();
    bool CreateDbhInfoWriter();
    bool SendCloseFdsDbhInfo();

    virtual bool CreateDbhSendNotifyWriter() = 0;
    virtual bool CreateDbhRecvNotifyReader() = 0;
    virtual bool Write(void* msg) = 0;

    std::atomic<bool> is_first_write_{true};
    const static uint32_t RINGBUFFER_LEN = 5;

protected:
    bool CreateDbhTopic();
    uint32_t GetDbhBlockSize(uint32_t msg_size);

protected:
    std::string origin_topic_name_;
    Publisher* fastdds_pub_ = nullptr;
    DomainParticipant* fastdds_participant_ = nullptr;
    const message_type_support_callbacks_t* msg_ts_callbacks_ = nullptr;
    MessageTypeSupport_cpp* msg_ts_ = nullptr;

    // used for dmabufheap transfer
    DataWriter* dbh_send_notify_writer_ = nullptr;
    Topic* dbh_send_notify_topic_ = nullptr;
    std::string dbh_send_notify_topic_name_;
    DataWriterListenerImpl* dbh_send_notify_writer_listener_ = nullptr;
    DataWriterQos dbh_send_notify_writer_qos_;
    StatusMask dbh_send_notify_writer_status_mask_;
    dal_builtin::msg::DalDmaBufHeapSendNotifyMsg dbh_send_notify_msg_;
    const message_type_support_callbacks_t* dbh_send_notify_msg_ts_callbacks_ = nullptr;

    DataReader* dbh_recv_notify_reader_ = nullptr;
    Topic* dbh_recv_notify_topic_ = nullptr;
    Subscriber* dbh_recv_notify_subscriber_ = nullptr;
    std::string dbh_recv_notify_topic_name_;
    const message_type_support_callbacks_t* dbh_recv_notify_msg_ts_callbacks_ = nullptr;

    DataWriter* dbh_info_writer_ = nullptr;
    Topic* dbh_info_topic_ = nullptr;
    std::string dbh_info_topic_name_;
    const message_type_support_callbacks_t* dbh_info_ts_callbacks_ = nullptr;

    dal_dmabufheap::BufferDescriptorRingBuffer* ringbuffer_ = nullptr;
    uint32_t estimated_size = 0;
    bool is_debug_print_enabled_ = false;
};

#endif  // _DAL_FASTDDS_DBH_WRITER_HPP_